<?php

// +--------------------------------------------------
// | Name： 星空框架 权限节点更新服务
// +--------------------------------------------------
// | Version: V5.0
// +--------------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +--------------------------------------------------
// | Time: 2021-04-21 11:12
// +--------------------------------------------------
declare(strict_types=1);

namespace app\common\services;

/**
 * @xk 权限节点更新服务
 */
class AuthRuleUpdate
{
    /**
     * @xk 获取权限节点列表
     *
     * @method getUpNodeService
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-21T11:14:29+080
     */
    public static function getUpNodeService()
    {
        // 获取系统节点
        $model_name = config('manage.model_name');
        $version = config('manage.version');
        $controllers = self::getControllers($model_name, $version); //获取控制器
        $controller = [];
        $unallow = ['login', 'index'];
        if ($controllers) {
            foreach ($controllers as $key => $value) {
                if (!in_array(strtolower($value), $unallow)) {
                    $controller[$key]['auth_name'] = self::getControllername($model_name, $value, $version);
                    $controller[$key]['auth_controller'] = strtolower($value);
                    $controller[$key]['auth_action'] = '';
                    $controller[$key]['linkurl'] = strtolower($value);
                    $controller[$key]['status'] = 1; //默认好使
                    $childs = [];
                    $controller[$key]['childs'] = [];
                    $action = [];
                    $actions = self::getActions($model_name, $value, $version);
                    if ($actions) {
                        foreach ($actions as $k => $val) {
                            $action[$k]['auth_name'] = self::getActionName($model_name, $value, $val, $version);
                            $action[$k]['auth_controller'] = strtolower($value);
                            $action[$k]['auth_action'] = strtolower($val);
                            $action[$k]['linkurl'] = strtolower($value.'/'.$val);
                            $action[$k]['status'] = 1; //默认好使
                        }
                    }
                    $controller[$key]['childs'] = $action;
                }
            }

            return $controller;
        }
    }

    /**
     * @xk 获取控制器
     *
     * @method getControllers
     *
     * @param [type] $model_name [description]
     * @param [type] $version    [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-21T11:16:38+080
     */
    public static function getControllers($module = 'xingkong', $version = 'v1')
    {
        if (empty($module)) {
            return null;
        }
        $module_path = app()->getBasePath().$module.'/controller/'.$version;  //控制器路径
        if (!is_dir($module_path)) {
            return null;
        }
        $module_path .= '/*.php';
        $ary_files = glob($module_path);
        foreach ($ary_files as $file) {
            if (is_dir($file)) {
                continue;
            } else {
                $files[] = basename($file, '.php');
            }
        }

        return $files;
    }

    /**
     * @xk 获取控制器名称
     *
     * @method getControllername
     *
     * @param string $module     [description]
     * @param [type] $controller [description]
     * @param string $version    [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-21T11:24:55+080
     */
    public static function getControllername($module = 'xingkong', $controller = '', $version = 'v1')
    {
        $cc_url = 'app\\'.$module.'\\controller\\'.$version.'\\'.$controller;
        $function = new \ReflectionClass($cc_url);
        $tmp = $function->getDocComment();
        if ($tmp) {
            $flag = preg_match_all('/@xk(.*?)\n/', $tmp, $tmp);
            $tmp = trim($tmp[1][0]);
            $tmp = '' != $tmp ? $tmp : '无';
        } else {
            $tmp = '无';
        }

        return $tmp;
    }

    /**
     * @xk 获取方法
     *
     * @method getActions
     *
     * @param string $module     [description]
     * @param [type] $controller [description]
     * @param string $version    [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-21T13:07:20+080
     */
    public static function getActions($module = 'xingkong', $controller = '', $version = 'v1')
    {
        if (empty($controller)) {
            return null;
        } //判断是否存在没有控制的情况
      $content = file_get_contents(app()->getBasePath().'/'.$module.'/controller/'.$version.'/'.$controller.'.php'); //获取相关文件
      preg_match_all("/.*?public.*?function(.*?)\(.*?\)/i", $content, $matches);
        $customer_functions = [];
        $functions = $matches[1];
        //排除部分方法
        $inherents_functions = ['_before_index', '_after_index', '_initialize', '__construct', 'display', 'show', 'fetch', 'buildHtml', 'assign', '__set', 'get', '__get', '__isset', '__call', 'error', 'success', 'ajaxReturn', 'redirect', '__destruct', '_empty'];
        foreach ($functions as $func) {
            $func = trim($func);
            if (!in_array($func, $inherents_functions)) {
                if (strlen($func) > 0) {
                    $customer_functions[] = $func;
                }
            }
        }

        return $customer_functions;
    }

    /**
     * @xk 获取方法名称
     *
     * @method getActionName
     *
     * @param string $module     [description]
     * @param string $controller [description]
     * @param string $action     [description]
     * @param string $version    [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-21T13:08:41+080
     */
    public static function getActionName($module = 'xingkong', $controller = '', $action = '', $version = 'v1')
    {
        $cc_url = 'app\\'.$module.'\\controller\\'.$version.'\\'.$controller;
        $func = new \ReflectionMethod($cc_url, $action);
        $tmp = $func->getDocComment();
        if ($tmp) {
            $flag = preg_match_all('/@xk(.*?)\n/', $tmp, $tmp);
            $tmp = trim($tmp[1][0]);
            $tmp = '' != $tmp ? $tmp : '无';
        } else {
            $tmp = '无';
        }

        return $tmp;
    }
}
